package action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import jxl.Sheet;
import jxl.Workbook;

import model.Choice;
import model.Fill;
import model.Judge;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

import dao.BasicDao;

@SuppressWarnings("serial")
public class QuestionAction extends ActionSupport {
	private BasicDao basicDao = new BasicDao();
	private Choice choice = new Choice();

	// 上传
	private static final int BUFFER_SIZE = 16 * 1024;
	private Fill fill = new Fill();
	private Judge judge = new Judge();
	// 保存文件的目录路径(通过依赖注入)
	private String savePath;
	// 文件标题
	private String title;

	private String type;

	// 上传文件域对象
	private File upload;

	// 上传文件类型
	private String uploadContentType;

	// 上传文件名
	private String uploadFileName;
	
	//提示信息
	private String message;
	public String export() {
		return SUCCESS;
	}

	public InputStream getInputStream() {
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
		HSSFSheet sheet = hssfWorkbook.createSheet("sheet1");
		HSSFRow row = sheet.createRow(0);
		HSSFCell cell = row.createCell((short) 0);
		File file = null;
		if (type.equals("Choice")) {
			List<Object> choosequestions = basicDao.getAllObjects(type);
			//设列名
			cell=row.createCell((short)0);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue("题干");
			cell=row.createCell((short)1);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue("A选项");
			cell=row.createCell((short)2);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue("B选项");
			cell=row.createCell((short)3);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue("C选项");
			cell=row.createCell((short)4);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue("D选项");
			cell=row.createCell((short)5);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue("正确答案");
			for (int i = 0; i < choosequestions.size(); ++i) {
				choice = (Choice) choosequestions.get(i);
				row = sheet.createRow(i + 1);
				cell = row.createCell((short) 0);
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				cell.setCellValue(choice.getContext());

				cell = row.createCell((short) 1);
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				cell.setCellValue(choice.getOptionA());

				cell = row.createCell((short) 2);
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				cell.setCellValue(choice.getOptionB());

				cell = row.createCell((short) 3);
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				cell.setCellValue(choice.getOptionC());

				cell = row.createCell((short) 4);
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				cell.setCellValue(choice.getOptionD());

				cell = row.createCell((short) 5);
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				cell.setCellValue(choice.getRightAnswer());
			}

			file = new File("choice.xls");
		}
		if (type.equals("Fill")) {
			@SuppressWarnings("unchecked")
			List<Object> fillquestions = basicDao.getAllObjects(type);
			//设列名
			cell=row.createCell((short)0);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue("题干");
			cell=row.createCell((short)1);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue("正确答案");
			for (int i = 0; i < fillquestions.size(); ++i) {
				fill = (Fill) fillquestions.get(i);
				row = sheet.createRow(i + 1);
				cell = row.createCell((short) 0);
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				cell.setCellValue(fill.getContext());

				cell = row.createCell((short) 1);
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				cell.setCellValue(fill.getRightAnswer());
			}

			file = new File("choice.xls");
		}

		if (type.equals("Judge")) {
			@SuppressWarnings("unchecked")
			List<Object> judgequestions = basicDao.getAllObjects(type);
			//设列名
			cell=row.createCell((short)0);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue("题干");
			cell=row.createCell((short)1);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue("正确答案");
			for (int i = 0; i < judgequestions.size(); ++i) {
				judge = (Judge) judgequestions.get(i);
				row = sheet.createRow(i + 1);
				cell = row.createCell((short) 0);
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				cell.setCellValue(judge.getContext());

				cell = row.createCell((short) 1);
				cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				cell.setCellValue(judge.getRightAnswer());
			}

			file = new File("choice.xls");
		}

		try {
			OutputStream os = new FileOutputStream(file);
			hssfWorkbook.write(os);
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		InputStream is = null;
		try {
			is = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return is;
	}

	public String getSavePath() {
		return savePath;
	}

	public String getTitle() {
		return title;
	}

	public String getType() {
		return type;
	}

	public File getUpload() {
		System.out.println(upload.getAbsolutePath());
		return upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public String in() {
		String dstPath = ServletActionContext.getServletContext().getRealPath(
				this.getSavePath())
				+ "//" + this.getUploadFileName();
		File dstFile = new File(dstPath);
		copy(this.upload, dstFile);
		//开始解析
		Workbook workbook = null;
		try {
			workbook = Workbook.getWorkbook(dstFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		{
			Sheet sheet = workbook.getSheet(0);
			sheet.getColumns();
			int rowCount = sheet.getRows();
			for (int i = 0; i < rowCount; i++) {
				if (type.equals("Choice")) {
					Choice choice=new Choice();
					if (cell_to_choice(sheet, i,choice)) {
						basicDao.save(choice);
					}else {
						setMessage("操作失败<br>原因：文件格式不匹配");
						return SUCCESS;
					}
				}
				if (type.equals("Fill")) {
					Fill fill=new Fill();
					if (cell_to_fill(sheet, i,fill)) {
						basicDao.save(fill);
					} else {
						setMessage("操作失败<br>原因：文件格式不匹配");
						return SUCCESS;
					}
				}
				if (type.equals("Judge")) {
					Judge judge=new Judge();
					if (cell_to_judge(sheet, i,judge)) {
						basicDao.save(judge);
					} else {
						setMessage("操作失败<br>原因：文件格式不匹配");
						return SUCCESS;
					}
					
				}
			}
			workbook.close();

		}
		setMessage("操作成功");
		return SUCCESS;
	}

	public Boolean cell_to_judge(Sheet sheet, int i,Judge judge) {
		try {
			judge.setId(Integer.valueOf(sheet.getCell(0, i).getContents()));
			judge.setContext(sheet.getCell(1, i).getContents());
			judge.setRightAnswer(sheet.getCell(2, i).getContents());
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public Boolean cell_to_fill(Sheet sheet, int i, Fill fill) {
		try {
			int temp=Integer.valueOf((sheet.getCell(0, i).getContents()));
			fill.setId(temp);
			fill.setContext(sheet.getCell(1, i).getContents());
			fill.setRightAnswer(sheet.getCell(2, i).getContents());
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	private static void copy(File src, File dst) {
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
			out = new BufferedOutputStream(new FileOutputStream(dst),
					BUFFER_SIZE);
			byte[] buffer = new byte[BUFFER_SIZE];
			int len = 0;
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != out) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public Boolean cell_to_choice( Sheet sheet,int i,Choice choice) {
		try {
			int temp=Integer.valueOf((sheet.getCell(0, i).getContents()));
			choice.setId(temp);
			choice.setContext((sheet.getCell(1, i).getContents()));
			choice.setOptionA(sheet.getCell(2, i).getContents());
			choice.setOptionB(sheet.getCell(3, i).getContents());
			choice.setOptionC(sheet.getCell(4, i).getContents());
			choice.setOptionD(sheet.getCell(5, i).getContents());
			choice.setRightAnswer(sheet.getCell(6, i).getContents());
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getMessage() {
		return message;
	}
}
